<template>
  <div class="register-container">
    
    <el-input placeholder="用户名" v-model="username"></el-input>
    <el-input placeholder="密码" v-model="password" show-password></el-input>
    <el-input placeholder="确认密码" v-model="confirmPassword" @blur="checkPassword" show-password></el-input>
    <el-input placeholder="名字" v-model="name"></el-input>
    <el-input placeholder="地址" v-model="address"></el-input>
    <span>性别：</span>
    <el-radio v-model="sex" :label="1" :value="男">男</el-radio>
    <el-radio v-model="sex" :label="0" :value="女">女</el-radio>
    <el-button type="primary" @click="re">返回</el-button>
    <el-button type="primary" @click="register">注册</el-button>
  </div>
</template>

<script>
import service from "../axios/axiosConfig"
export default {
    data() {
        return {
            username: "",
            password: "",
            name: "",
            address: "",
            confirmPassword: "",
            sex: 1,
        }
    },
  methods: {
      async register() {
          if (this.confirmPassword != this.password) {
              alert("两次密码不一致")
              return
          
        }
      const responseBody = {
        username: this.name,
        password: this.password,
          location: this.address,
        name: this.name,
        gender: this.sex===1? "男":"女"
      };
          const response = await service.post("http://localhost:8080/common/register", responseBody)
          sessionStorage.setItem("username", this.username)
      sessionStorage.setItem("password", this.password)
     console.log(response)
      },
      //检查确认密码是否一致
    checkPassword(){
      if(this.password!=this.confirmPassword){
          this.$message.error("两次密码不一致")
        this.confirmPassword=""
      }
      },
      re() {
        this.$router.push("/login")
      }
  }
}
</script>